package com.example.dota2draftsimulator;

import java.io.IOException;

import com.example.dota2draftsimulator.fragments.SettingFrament;
import com.example.dota2draftsimulator.view.CustomButton;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.TextView;

public class SplashActivity extends Activity {

	protected static final int TIMER_RUNTIME = 8000; 
	private TextView loadingTextView;
	private CustomButton startButton;
	private CustomButton heroesDBButton;
	private CustomButton helpButton;
	private CustomButton exitButton;
	private MediaPlayer bgMediaPlayer;
	private SettingFrament settings;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		Typeface type = Typeface.createFromAsset(getAssets(),"OPTIMUSPRINCEPSSEMIBOLD.TTF"); 

		try {
			AssetFileDescriptor afd = getAssets().openFd("dota2_bg_music.mp3");
			bgMediaPlayer = new MediaPlayer();
			bgMediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
			bgMediaPlayer.prepare();
			bgMediaPlayer.setLooping(true);
			bgMediaPlayer.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		loadingTextView = (TextView) findViewById(R.id.loading_text);
		loadingTextView.setTypeface(type); 
		loadingTextView.setText("Loading");
		loadingTextView.setTextSize(20);	

		
		startButton = (CustomButton) findViewById(R.id.start_button);
		startButton.setTextForButton("Start Draft");
		startButton.setVisibility(View.INVISIBLE);
		startButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				settings = new SettingFrament();
				settings.show(getFragmentManager(), "player");
			}
		});

		heroesDBButton = (CustomButton) findViewById(R.id.heroes_db_button);
		heroesDBButton.setTextForButton("Heroes Database");
		heroesDBButton.setVisibility(View.INVISIBLE);

		helpButton = (CustomButton) findViewById(R.id.help_button);
		helpButton.setTextForButton("Help");
		helpButton.setVisibility(View.INVISIBLE);

		exitButton = (CustomButton) findViewById(R.id.exit_button);
		exitButton.setTextForButton("Exit");
		exitButton.setVisibility(View.INVISIBLE);
		exitButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});

		new CountDownTimer(TIMER_RUNTIME, 1000) {
			int tickTime = 0;
			@Override
			public void onTick(long millisUntilFinished) {
				// TODO Auto-generated method stub
				tickTime++;

				String loadingText = loadingTextView.getText().toString();
				if(loadingText.equals("Loading...")) {
					loadingText = "Loading";
				}

				loadingText = loadingText + ".";
				loadingTextView.setText(loadingText);

				if(tickTime == 7) {
					hideLoadingAnimation();
				}
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				showingAnimation();
			}
		}.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.splash, menu);
		return true;		
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
		int heroesDBButtonSize = heroesDBButton.getMeasuredWidth();
		startButton.setWidthForButton(heroesDBButtonSize);
		helpButton.setWidthForButton(heroesDBButtonSize);
		exitButton.setWidthForButton(heroesDBButtonSize);
	}

	public void showingAnimation() {
		Animation animation = new AlphaAnimation(0.0f, 0.5f);
	    animation.setDuration(100);
	    startButton.startAnimation(animation);
		startButton.setVisibility(View.VISIBLE);
		heroesDBButton.startAnimation(animation);
		heroesDBButton.setVisibility(View.VISIBLE);
		helpButton.startAnimation(animation);
		helpButton.setVisibility(View.VISIBLE);
		exitButton.startAnimation(animation);
		exitButton.setVisibility(View.VISIBLE);
	}
	
	public void hideLoadingAnimation() {
		Animation animation = new AlphaAnimation(0.5f, 0.0f);
	    animation.setDuration(100);
	    loadingTextView.startAnimation(animation);
	    loadingTextView.setVisibility(View.INVISIBLE);
	}

}
